/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package controller.javaBeans;

import java.text.DateFormat;
import java.util.Date;

import model.Vocabulary;
import model.dataContainer.ChangerequestContainer;

import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;

import controller.sesameDataConnector.ChangerequestConnector;

public class ChangerequestBean {

	
	private String lable;
	private String comment;
	private URI userid;
	private URI element;
	private URI state;
	private Date id;
	
	public ChangerequestBean() {
		lable = "";
		comment = "";
		userid = null;
		element = null;
		state = null;
		id = null;
	}

	
	
	public boolean insert(String id, URI user) {
		try {
			model.dataContainer.ChangerequestContainer cr = new ChangerequestContainer();
			cr.setComment(this.comment);
			cr.setElement(new URIImpl((Vocabulary.NAMESPACE.stringValue()) + id ));
			cr.setId(new Date());
			cr.setLable(this.lable);
			cr.setState(Vocabulary.REQUESTSTATEOPEN);
			cr.setUserid(user);
			
			ChangerequestConnector.insert(cr, id);
		} catch (Exception e){

		}
		return true;
	}
	
	public String getStateString(URI uri) {
		
		if (uri.equals(Vocabulary.REQUESTSTATECLOSED))
			return "Closed";
		else if (uri.equals(Vocabulary.REQUESTSTATEOPEN))
			return "Open";
		else if (uri.equals(Vocabulary.REQUESTSTATEREOPENED))
			return "Reopened";
		return "Unknown";
	}
	
	
	public String constructChangerequestTable (ChangerequestContainer cr, String requesturi, String id) {
		String ret = "";
		try {
			ret += "	<tr>";
			ret += "		<td>" + DateFormat.getDateInstance(DateFormat.SHORT).format(cr.getId());
	 		ret += "		<td>" + cr.getLable() + "</td>\n";
			ret += "		<td>" + getStateString(cr.getState()) + "</td>\n";
			ret += "		<td>" + cr.getUserid().getLocalName() + "</td>\n";
	//		ret += "		<td> <form action=\"" + requesturi +"\" mehtod=\"POST\" name=\"viewchangerequestform\"><input type=\"hidden\" name=\"id\" value=\"" + id + "\" /><input type=\"hidden\" name=\"action\" value=\"show_changerequest\"><a href=\"javascript:document.viewchangerequestform.submit();\">view</a></form></td>\n";
			
			ret += "		<td>" + controller.HelperFunctions.getLinkedChangeRequest( cr.getElement().stringValue(), Long.toString(cr.getId().getTime() ) ) + "</td>\n"; 
			ret += "	</tr>\n";	
		} catch (Exception e){

		}
		
		return ret;
		
	}
	
	
	public String getAllChangerequests(String id, String requesturi) {
		
		URI elementId = new URIImpl((Vocabulary.NAMESPACE.stringValue()) + id );
		String ret ="";
		java.util.ArrayList<ChangerequestContainer> cr;
		cr = ChangerequestConnector.selectAllOfElement(elementId);
		if (cr.size() == 0) {
			ret = "<p>There are no Change Requests for this element.</p>";
		}
		else {
			ret += "<table class=\"sortable\">\n";
			ret += "	<th>Date</th> <th>Title</th> <th>State</th> <th>User</th> <th class=\"sorttable_nosort\"> </th>";	
			for ( int i = 0; i < cr.size(); i++) {
				ret += constructChangerequestTable(cr.get(i), requesturi, id);
			}
			ret += "</table>\n";
		}
		
		
		return ret;
	}
	
	
	public String getStateChanger(ChangerequestContainer cr, String crid, String elementid) {
		
		String ret ="";

		if (cr.getState().equals(Vocabulary.REQUESTSTATEOPEN) || cr.getState().equals(Vocabulary.REQUESTSTATEREOPENED)) {
			
			ret = "<form action=\"" + Vocabulary.NAMESPACE.getLocalName() + "\" method=\"POST\" name=\"formChangeState\"><input type=\"hidden\" name=\"state_action\" value=\"change_state_close\"/><input type=\"hidden\" name=\"crid\" value=\"" + crid + "\"/><a href=\"javascript:getStateChangeMessage( document.formChangeState );\">[close]</a></form>";
		}
		else if (cr.getState().equals(Vocabulary.REQUESTSTATECLOSED)) {
			
			ret = "<form action=\"" + Vocabulary.NAMESPACE.getLocalName() + "\" method=\"POST\" name=\"formChangeState\"><input type=\"hidden\" name=\"state_action\" value=\"change_state_reopen\"/><input type=\"hidden\" name=\"crid\" value=\"" + crid + "\"/><a href=\"javascript:getStateChangeMessage( document.formChangeState );\">[reopen]</a></form>";
		}
		
		return ret;
	}
	
	public String getChangeRequest(String crid, String elementid, boolean loggendIn) {
		String ret = "";
		
		URI uri = new URIImpl((Vocabulary.NAMESPACE.stringValue()) + elementid + "/ChangeRequest/" + crid);
		ChangerequestContainer cr = ChangerequestConnector.select(uri);
		
		
		ret += "<div id=\"maininfos\">\n";
		ret += "<div id=\"maininfosinner\">\n";
		
		
		ret += "<div class=\"elementfield\">";
		ret += "<h3 class=\"fieldtitle\">Title</h3>\n";
		ret += "<div class=\"fieldcontent\">" + cr.getLable() + "</div>\n";
		ret += "</div>";
		
		ret += "<div class=\"elementfield\">";
		ret += "<h3 class=\"fieldtitle\">State</h3>\n";
		if (loggendIn)
			ret += "<div class=\"fieldcontent\">" + this.getStateString(cr.getState()) + getStateChanger(cr, crid, elementid)+  "</div>\n";
		else
			ret += "<div class=\"fieldcontent\">" + this.getStateString(cr.getState()) + "</div>\n";
		ret += "</div>";

		ret += "<div class=\"elementfield\">";
		ret += "<h3 class=\"fieldtitle\">Comment</h3>\n";
		ret += "<div class=\"fieldcontent\">" + cr.getComment() + "</div>\n";
		ret += "</div>";
		
		
		ret += "</div>\n";
		ret += "</div>\n";
		
		
		
		

		ret += "<div id=\"additionalinfos\">\n";
		ret += "<div id=\"additionalinfosinner\">\n";

		
		ret += "<div class=\"elementfield\">";
		ret += "<h3 class=\"fieldtitle\">Date, Time</h3>\n";
		ret += "<div class=\"fieldcontent\">" + cr.getId().toString() + "</div>\n";
		ret += "</div>";

		ret += "<div class=\"elementfield\">";
		ret += "<h3 class=\"fieldtitle\">User</h3>\n";
		ret += "<div class=\"fieldcontent\">" + cr.getUserid().getLocalName() + "</div>\n";
		ret += "</div>";
		

		ret += "</div>\n";
		ret += "</div>\n";
		
		
		

		ret += "<div class=\"clearer\">&nbsp;</div>";
		
		return ret;
	}
	
	
	public boolean closeChangeRequest(String crid, String elementid) {
		URI uri = new URIImpl((Vocabulary.NAMESPACE.stringValue()) + elementid + "/ChangeRequest/" + crid);
	
		return ChangerequestConnector.closeChangeState(uri);
	}
	
	
	public boolean openChangeRequest(String crid, String elementid) {
		URI uri = new URIImpl((Vocabulary.NAMESPACE.stringValue()) + elementid + "/ChangeRequest/" + crid);
	
		return ChangerequestConnector.openChangeState(uri);
	}
	
	
	public String getComment() {
		return comment;
	}

	public URI getElement() {
		return element;
	}

	public Date getId() {
		return id;
	}

	public String getLable() {
		return lable;
	}

	public URI getState() {
		return state;
	}

	public URI getUserid() {
		return userid;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public void setElement(URI element) {
		this.element = element;
	}

	public void setId(Date id) {
		this.id = id;
	}

	public void setLable(String lable) {
		this.lable = lable;
	}

	public void setState(URI state) {
		this.state = state;
	}

	public void setUserid(URI userid) {
		this.userid = userid;
	}
}
